package com.goods.business.service.imp;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import com.google.j2objc.annotations.AutoreleasePool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;


@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductStockMapper productStockMapper;


    /**
     * 物资资料分页列表
     */
    @Override
    public PageVO<ProductVO> findProductList(int pageNum, int pageSize, String name, Integer categoryId, Integer status) {
        //生成查询对象

        List<Product> products = productMapper.findProductList(name,categoryId,status);
        //转换成VO对象
        List<ProductVO> productVOList = new ArrayList<>();
        if (products != null){
            for (Product product : products) {
                ProductVO productVO = new ProductVO();
                BeanUtils.copyProperties(product,productVO);
                productVOList.add(productVO);
            }

            //封装分页条件
            PageHelper.startPage(pageNum,pageSize);
            PageInfo<ProductVO> pageInfo = new PageInfo<>(productVOList);
            //返回PageVO对象
            return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
        }

       return null;
    }

    /**
     * 添加物资
     * @param productVO
     * @return
     */
    @Override
    public void add(ProductVO productVO) {
        if (productVO != null){
            //交换
            Product product = new Product();
            BeanUtils.copyProperties(productVO,product);
            //生成随机id
            product.setPNum(UUID.randomUUID().toString());
            //set,物资类别值
            product.setOneCategoryId(productVO.getCategoryKeys()[0]);
            product.setTwoCategoryId(productVO.getCategoryKeys()[1]);
            product.setThreeCategoryId(productVO.getCategoryKeys()[2]);
            product.setStatus(2);
            //时间
            product.setCreateTime(new Date());
            product.setModifiedTime(new Date());

            //添加到数据库
            productMapper.insert(product);
        }
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @Override
    public Product edit(Long id) {
        if (id != null){
            Product product = productMapper.selectByPrimaryKey(id);
            if (product != null){
                return product;
            }
        }
        return null;
    }

    /**
     * 放入回收站
     * @param id
     * @return
     */
    @Override
    public void remove(Long id) {
        Product product  = productMapper.selectByPrimaryKey(id);
        if (product != null){
            product.setStatus(1);
            product.setModifiedTime(new Date());
            productMapper.updateByPrimaryKey(product);
        }
    }

    /**
     * 恢复回收站
     * @param id
     * @return
     */
    @Override
    public void back(Long id) {
        Product product  = productMapper.selectByPrimaryKey(id);
        if (product != null){
            product.setStatus(0);
            product.setModifiedTime(new Date());
            productMapper.updateByPrimaryKey(product);
        }
    }

    /**
     * 删除回收站
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
        if (id != null){
            productMapper.deleteByPrimaryKey(id);
        }
    }


    /**
     * 查询所有物资
     */
    @Override
    public PageVO<ProductStockVO> findProducts(int pageNum, int pageSize, Integer status) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        //查询条件
        criteria.andEqualTo("status",status);
        //查询
        List<Product> productList = productMapper.selectByExample(example);
        if (productList != null){
            List<ProductStockVO> productVOList = new ArrayList<>();
            for (Product product : productList) {
                ProductStockVO productStockVO = new ProductStockVO();
                BeanUtils.copyProperties(product,productStockVO);

                //去中间表查询数量
                Example example1 = new Example(ProductStock.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("pNum",product.getPNum());
                ProductStock productStock = productStockMapper.selectOneByExample(example1);
                //赋值
                productStockVO.setStock(productStock.getStock());

                productVOList.add(productStockVO);
            }

            //构建分页信息
            PageHelper.startPage(pageNum,pageSize);
            PageInfo<ProductStockVO> pageInfo = new PageInfo<>(productVOList);
            //返回数据
            return new PageVO<ProductStockVO>(pageInfo.getTotal(),pageInfo.getList());
        }
        return null;
    }


    /**
     * 物资库存图
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVO<ProductStockVO> findProductStocks(int pageNum, int pageSize) {
        //返回的对象
        List<ProductStockVO> productVOList = new ArrayList<>();

        //封装分页对象
        List<Product> productList = productMapper.selectAll();
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Product> pageInfo = new PageInfo<>(productList);

        for (Product product : pageInfo.getList()) {
            //封装数据
            ProductStockVO productStockVO = new ProductStockVO();
            BeanUtils.copyProperties(product,productStockVO);
            //查询库存数量
            Example example = new Example(ProductStock.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("pNum",product.getPNum());
            ProductStock productStock = productStockMapper.selectOneByExample(example);
            if (productStock != null){
                productStockVO.setStock(productStock.getStock());
            }else {
                productStockVO.setStock(0L);
            }
            //封装到集合中
            productVOList.add(productStockVO);
        }

        return new PageVO(pageInfo.getTotal(),productVOList);
    }


    /**
     * 获取所有库存
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<Map<String, Object>> findAllStocks(int pageNum, int pageSize) {
        //返回数据
        List<Map<String, Object>> list = new ArrayList<>();
        //调用本类方法获取数据
        PageVO<ProductStockVO> productStocks = this.findProductStocks(pageNum, pageSize);
        //循环
        for (ProductStockVO productStockVO : productStocks.getRows()) {
            //封装数据
            Map<String, Object> map = new HashMap<>();
            map.put("name",productStockVO.getName());
            map.put("stock",productStockVO.getStock());
            //传入List
            list.add(map);
        }
        return list;
    }
}
